/**
 * 文件路径名转格式：用给定的字符串切分命名，例:'./a/b.js' - > 'a_b'
 * @param {*} str String  要被转换的字符串
 * @param {*} param String  以该字符切分
 * @returns String
 */
export function restPathByParams(str: any, param: any) {
  let newStr = str
  newStr = newStr.replace(/.ts$/g, '')
  newStr = newStr.replace(/\.+\//g, '')
  newStr = newStr.replace(/\//g, param)
  return newStr
}
/**
 * 文件路径名转格式：转化为小驼峰例子
 * @param {*} str String 要被转换的字符串
 * @returns String
 */
export function resetPathHump(str: string) {
  let newStr = str
  if (newStr.indexOf('/') === 0) newStr = newStr.substr(1)
  newStr = newStr.replace(/.ts$/g, '')
  newStr = newStr.replace(/\.+\//g, '')
  newStr = newStr.replace(/\/{1}([a-zA-Z]{1})/g, ($0: any, $1: string) => $1.toUpperCase())
  newStr = newStr.replace(/\//g, '')
  return newStr
}

// 根据颜色的十六进制获取反向色值
export function colorReverse(oldColor: any) {
  oldColor = '0x' + oldColor.replace(/#/g, '');
  const str = '000000' + (0xFFFFFF - oldColor).toString(16);
  return '#'+ str.substring(str.length - 6, str.length);
}

// 对象数组深拷贝
// export function cloneDeep(par:Array<{[x:string]: any}>) {
//   const newArr: Array<any> = []
//   par.forEach((val) => {
//     const obj = {
//       ...val
//     }
//     if(val.children && val.children.length){
//       obj.children = cloneDeep(val.children)
//     }
//     if(!val.url){
//       newArr.push(obj)
//     }
//   })
//   return newArr
// }


// 树结构转扁平化
export function goFlat(par:Array<any>) {
  const stack = [...par]
  const newArr: {[x:string]: any} = {}
  while(stack.length){
    const obj = stack.pop()
    if(obj.children && obj.children.length){
      newArr[obj.id] = []
      obj.children.forEach((val: any) => {
        newArr[obj.id].push(val)
        if(val.children) stack.push(val)
      })
    }
  }
  return newArr
}

export default {}
